package com.wit.health.UI;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.Toast;


import com.wit.health.R;
import com.wit.health.pojo.User;
import com.wit.health.util.NetUtil;

/**  
* 注册界面  
*  
* @Author liaozhaoping 
* @Date 2024/5/10 15:50
*/
public class RegisterActivity extends AppCompatActivity {

    private Button btn_Register;
    private Button btn_back;

    private EditText etUsername;

    private EditText etPassword;

    private EditText etPassword2;

    private EditText etEmail;

    private CheckBox cb_Agreement;

    private NumberPicker numberPickerWeight;

    private NumberPicker numberPickerHeight;

    private NumberPicker numberPickerShoeSize;

    private RadioButton radioButtonMale;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        btn_Register.setOnClickListener(v -> {
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            String password2 = etPassword2.getText().toString();
            String email = etEmail.getText().toString();
            // 在这里处理注册逻辑
            //判断电子邮箱是否合法
            if (!email.matches("[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)+")) {
                Toast.makeText(RegisterActivity.this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(username)){
                Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(password) || TextUtils.isEmpty(password2)){
                Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if(!password.equals(password2)){
                Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            if(!cb_Agreement.isChecked()) {
                Toast.makeText(RegisterActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
                return;
            }
            // 将所有用户信息以json格式发送给后端服务器

            int gender = radioButtonMale.isChecked() ? 0 : 1;
            int weight = numberPickerWeight.getValue();
            int height = numberPickerHeight.getValue();
            int shoeSize = numberPickerShoeSize.getValue();

            User user = new User(username, email, password, gender, weight, height, shoeSize);

            // 服务器返回注册成功或失败
            NetUtil.sendUserInfoToServer(RegisterActivity.this, user);

        });
        btn_back.setOnClickListener(v -> {
            finish();
        });

    }

    private void initView() {
        btn_Register = findViewById(R.id.btn_register);
        btn_back = findViewById(R.id.btn_back);
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.et_password);
        etPassword2 = findViewById(R.id.et_password2);
        etEmail = findViewById(R.id.et_Email);
        cb_Agreement = findViewById(R.id.cb_Agreement);

        numberPickerWeight = findViewById(R.id.numberPicker_weight);
        numberPickerWeight.setMinValue(30);
        numberPickerWeight.setMaxValue(150);
        numberPickerWeight.setValue(70);

        numberPickerHeight = findViewById(R.id.numberPicker_height);
        numberPickerHeight.setMinValue(100);
        numberPickerHeight.setMaxValue(220);
        numberPickerHeight.setValue(170);

        numberPickerShoeSize = findViewById(R.id.numberPicker_shoeSize);
        numberPickerShoeSize.setMinValue(30);
        numberPickerShoeSize.setMaxValue(50);
        numberPickerShoeSize.setValue(40);

        radioButtonMale = findViewById(R.id.radioButton_male);
    }

    private void showToast(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }


}